package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gallant
 * @create 2020-07-15 11:08
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    // 查询所有
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    // 分页查询
    public PageResult queryPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<Role> roleList = roleDao.findByCondition(queryString);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    // 添加角色
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        this.setRoleAndPermission(role.getId(), permissionIds);
        this.setRoleAndMenu(role.getId(), menuIds);
    }

    @Override
    public List<Integer> findPermissionIdByRoleId(Integer id) {
        return roleDao.findPermissionIdByRoleId(id);
    }


    public List<Integer> findMenuIdByRoleId(Integer id) {
        return roleDao.findMenuIdByRoleId(id);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.update(role);
        roleDao.deletePermissions(role.getId());
        roleDao.deleteMenus(role.getId());
        roleDao.addPermissions(role.getId(),permissionIds);
        roleDao.addMenuIds(role.getId(),menuIds);
    }

    @Override
    public void delete(Integer id)  throws Exception {
        // 先根据id查询用户,如果有用户,返回不能删除
        List<User> list = roleDao.findUserByRoleId(id);
        System.out.println(list);
        // 如果没有用户使用这个角色
        if (list == null || list.size() <=0) {
            // 那就先取消菜单和权限的关联关系
            roleDao.deletePermissions(id);
            roleDao.deleteMenus(id);
            // 然后再删除角色
            roleDao.delete(id);
        }else {
            throw new RuntimeException();
        }
    }

    // 给中间表添加数据
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {

        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }

    }

    // 给中间表添加数据
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }


}
